package com.wonder.app.controller;


import com.wonder.config.MqttProviderConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器,实现消息的发布功能
 */
@Controller
@RequestMapping("/mqtt")
public class SendController {
    @Autowired
    private MqttProviderConfig provideClient;

    @Value("${spring.mqtt.client.id}" + "sy")
    private String clientId;

    @RequestMapping("/sendMessage")
    @ResponseBody
    public String sendMessage(Integer qos,boolean retained,String topic,String message){
        try {
            provideClient.publish(qos,retained,topic,message);
            return "发送成功";
        } catch (Exception e){
            e.printStackTrace();
            return "发送失败";
        }
    }


    @RequestMapping("connect")
    @ResponseBody
    public String connect(){
        provideClient.connect();
        return clientId + "连接到服务器";
    }
}
